//
//  UIBarButtonItem+Extension.swift
//  WeiboSourceCode
//
//  Created by 周星宇 on 15/12/26.
//  Copyright © 2015年 cn.weibo. All rights reserved.
//

import UIKit


extension UIBarButtonItem {
    
    // 在分类中 只可以定义便利构造方法
    // 如果想让外界可以不传某个参数  可以给定一个默认值
    convenience init(imageName: String? = nil, title: String? = nil, action: Selector,target: AnyObject){
        self.init()
        
        let btn = UIButton()
        btn.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        
        if imageName != nil {
            
            btn.setImage(UIImage(named: imageName!)?.imageWithRenderingMode(.AlwaysOriginal), forState: UIControlState.Normal)
           
            btn.setImage(UIImage(named: "\(imageName!)_highlighted")?.imageWithRenderingMode(.AlwaysOriginal), forState: UIControlState.Highlighted)
            
        }
        if title != nil {
            
            btn.setTitle(title, forState: .Normal)
            btn.setTitleColor(UIColor(white: 80/255, alpha: 1), forState: .Normal)
            btn.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
            btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        }
        // 设置按钮大小
        btn.sizeToFit()
        
        customView = btn
        
    }
    
    
}

